//
//  WYRThreeViewController.m
//  10-21-快速搭建框架
//
//  Created by lkx0 on 16/10/21.
//  Copyright © 2016年 lkx0. All rights reserved.
//

#import "WYRThreeViewController.h"
#import "WYRFriend.h"
#import "WYRFriendCell.h"
#import "WYRFriendGroup.h"


@interface WYRThreeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tbView;

//分组模型数据
@property(nonatomic,strong)NSArray *groups;
@end

@implementation WYRThreeViewController

#pragma mark - 懒加载数据
-(NSArray *)groups{
    if (_groups==nil) {
        //加载，不为空不加载
        //1.获取文件路径
        NSString *path=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];
        
        //2.读取文件内容到一个数据中
        NSArray *arraydicts=[NSArray arrayWithContentsOfFile:path];
        
        //3.创建一个可变数组来保存分组模型对象
        NSMutableArray *arrayModels=[NSMutableArray array];
        
        //4.将plist文件中读取出来的分组模型对象添加到可变数组中
        for (NSDictionary * dict in arraydicts) {
            //创建分组模型对象来接受数据
            WYRFriendGroup * model=[WYRFriendGroup friendGroupWithDict:dict];
            [arrayModels addObject:model];
        }
        //5.将保存好的分组模型赋值给属性
        _groups=arrayModels;
    }
    return _groups;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    //创建分组的tableView
    _tbView=[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    _tbView.backgroundColor=[UIColor yellowColor];
    
    
    
    
    //设置代理
    _tbView.dataSource=self;
    _tbView.delegate=self;
    [self.view addSubview:_tbView];
    
    
    //打印分组数据
    NSLog(@"分组数据:%@",_groups);

    // Do any additional setup after loading the view.
}


#pragma mark - 数据源方法
//多少行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _groups.count;
}

//每一行多少个好友
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WYRFriendGroup *group=_groups[section];
    return group.friends.count;
    
//    //判断该组目前是展开状态还是收起状态
//    if (group.isVisible) {
//        return group.friends.count;
//    }else{
//        return 0;
//    }
    
    //return 5;
}

//每一行的cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//创建可重用cell
//    static NSString *ID=@"friendsID";
//    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
//    if (cell==nil) {
//        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
//        
//    }
    
    //分组模型
    WYRFriendGroup *group=_groups[indexPath.section];
    //好友模型
    WYRFriend *friend=group.friends[indexPath.row];
    WYRFriendCell * cell=[[WYRFriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    cell.friendModel=friend;
    
    
    

    return cell;

}

//设置每一组的组标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"我的好友";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
